package com.huaxin.hxcloud.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.BigDeviceDTO;
import com.huaxin.common.Entity.ImageInfo;
import com.huaxin.common.Entity.SubDevice;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/12/29.
 */

public class BigDeviceInfoActivity extends BaseActivity {


    DeviceAdapter mDeviceAdapter;
    ImgAdapter    mImgAdapter;
    @BindView(R.id.tv_type)
    TextView       tvType;
    @BindView(R.id.tv_weight)
    TextView       tvWeight;
    @BindView(R.id.tv_brand)
    TextView       tvBrand;
    @BindView(R.id.tv_model)
    TextView       tvModel;
    @BindView(R.id.tv_power)
    TextView       tvPower;
    @BindView(R.id.tv_check_cycle)
    TextView       tvCheckCycle;
    @BindView(R.id.tv_product_date)
    TextView       tvProductDate;
    @BindView(R.id.tv_purchase_date)
    TextView       tvPurchaseDate;
    @BindView(R.id.more)
    TextView       more;
    @BindView(R.id.toolbar)
    Toolbar        toolbar;
    @BindView(R.id.appbar)
    AppBarLayout   appbar;
    @BindView(R.id.tv_album)
    TextView       tvAlbum;
    @BindView(R.id.ls_img)
    RecyclerView   lsImg;
    @BindView(R.id.rl_album)
    RelativeLayout rlAlbum;
    @BindView(R.id.list_view)
    RecyclerView   devicelistView;
    @BindView(R.id.ll_device)
    LinearLayout   llDevice;
    private List subDevicedatas = new ArrayList<>();
    private List imgDatas       = new ArrayList<>();
    private String       mDeviceCode;
    private BigDeviceDTO mBigDevice;
    private static final String TAG = "BigDeviceInfoActivity";
    private View emptyView;

    public static void start(Context context, String deviceCode) {
        Intent intent = new Intent(context, BigDeviceInfoActivity.class);
        intent.putExtra(Constants.IK_DEVICE_CODE, deviceCode);
        context.startActivity(intent);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        mDeviceCode = extras.getString(Constants.IK_DEVICE_CODE);

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_device;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        super.initView();
        setSupportActionBar(toolbar);
        toolbar.setTitle("设备详情");
        mImgAdapter = new ImgAdapter(R.layout.item_device_image, imgDatas);
        mDeviceAdapter = new DeviceAdapter(R.layout.item_device_s, subDevicedatas);
        devicelistView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL,
                false));
        devicelistView.setAdapter(mDeviceAdapter);
        lsImg.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,
                false));
        lsImg.setAdapter(mImgAdapter);

        emptyView = mInflater.inflate(R.layout.item_empty_nopic_center, (ViewGroup) lsImg.getParent(), false);

       // updateView();
        mDeviceAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                switch (view.getId()){
                    case  R.id.tv_detail:
                        SubDevice data = (SubDevice) subDevicedatas.get(position);
                        SmallDeviceInfoActivity.start(BigDeviceInfoActivity.this, data,mBigDevice.getDeviceName());
                        break;
                }
            }
        });
        mDeviceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                SubDevice data = (SubDevice) subDevicedatas.get(position);
                SmallDeviceInfoActivity.start(BigDeviceInfoActivity.this, data,mBigDevice.getDeviceName());
            }
        });

    }

    @Override
    public void requestData() {
        ApiRequestData(LOADING);
    }

    public void ApiRequestData(int status) {
        updateLoadStatus(status);
        Call<BigDeviceDTO> call = HxTransmitRequestHelper.getRequestApi().deviceInfo(mDeviceCode);
        autoClose(call);

        Log.d(TAG, "ApiRequestData:1----> " + call.request().url());
        call.enqueue(new Callback<BigDeviceDTO>() {
            @Override
            public void onResponse(Call<BigDeviceDTO> call, Response<BigDeviceDTO> response) {
                if (response.isSuccessful() && response.body() != null) {
                    BigDeviceDTO bigDevice = response.body();
                    if (bigDevice != null) {
                        updateLoadStatus(LOAD_SUCCESS);
                        mBigDevice = bigDevice;
                        updateView();
                    } else {
                        updateLoadStatus(LOAD_ERROR);
                        ToastUtil.showToast(bigDevice.getMsg());
                    }

                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<BigDeviceDTO> call, Throwable t) {
                Log.d(TAG, "onFailure: --->" + t);
                updateLoadStatus(LOAD_FAILURE);
            }
        });

    }

    private void updateView() {
        if (mBigDevice == null) {
            return;
        }
        toolbar.setTitle(mBigDevice.getDeviceName());
        loadText("设备类型 ", tvType, mBigDevice.getDeviceType(), "暂无");
        loadText("品牌 ", tvBrand, mBigDevice.getBrand(), "暂无");
        loadText("点检周期 ", tvCheckCycle, mBigDevice.getCheckPointCycle(), "暂无");
        loadText("型号 ", tvModel, mBigDevice.getModel(), "暂无");
        loadText("额定功率 ", tvPower, mBigDevice.getPower(), "暂无");
        if (mBigDevice.getProductDate() !=0){
            loadText("生产日期 ", tvProductDate, dateFormat(mBigDevice.getProductDate(),
                    "yyyy-MM-dd"), "暂无");
        }else {
            loadText("生产日期 ", tvProductDate, "","暂无");
        }
        loadText("重量 ", tvWeight, String.valueOf(mBigDevice.getWeight()), "暂无");
        if (mBigDevice.getPurchaseDate() != 0){
            loadText("购买日期 ", tvPurchaseDate, dateFormat(mBigDevice.getPurchaseDate(),
                    "yyyy-MM-dd"), "暂无");
        }else{
            loadText("购买日期 ", tvPurchaseDate, "", "暂无");
        }


        List<ImageInfo> imgs = mBigDevice.getImgUrl();
        imgDatas.clear();
        if (imgs == null || imgs.size() == 0) {
            mImgAdapter.setEmptyView(emptyView);
        } else {
            imgDatas.addAll(imgs);
            mImgAdapter.setNewData(imgDatas);
        }

        List<SubDevice> devices = mBigDevice.getSubDeviceList();
        subDevicedatas.clear();
        if (devices != null && devices.size() > 0) {
            //subDevicedatas.add(Constants.EMPTY_VIEW);
            subDevicedatas.addAll(devices);
        }
        mDeviceAdapter.setNewData(subDevicedatas);
    }


    @OnClick({R.id.more, R.id.rl_album})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.more:
                RepairRecordActivity.start(this, mDeviceCode);
                break;
            case R.id.rl_album:
                ImageActivity.showDeviceImg(this, mDeviceCode);
                break;
        }
    }


    protected class ImgAdapter extends BaseQuickAdapter<ImageInfo, BaseViewHolder> {

        public ImgAdapter(int layoutResId, @Nullable List<BaseViewHolder> data) {
            super(R.layout.item_device_image, imgDatas);
        }


        @Override
        protected void convert(BaseViewHolder baseViewHolder, ImageInfo imageInfo) {
            ImageView ivDevice = baseViewHolder.getView(R.id.iv_device);
            loadNetImg(ivDevice, imageInfo.getImgUrl());
        }
    }

    protected class DeviceAdapter extends BaseQuickAdapter<SubDevice, BaseViewHolder> {

        public DeviceAdapter(int layoutResId, @Nullable List<SubDevice> data) {
            super(R.layout.item_device_s, subDevicedatas);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, SubDevice subDevice) {

            TextView tvName = baseViewHolder.getView(R.id.tv_name);
            TextView tvModel = baseViewHolder.getView(R.id.tv_model);//型号
            TextView tv_check_cycle = baseViewHolder.getView(R.id.tv_check_cycle);//点检周期
            TextView tvPower = baseViewHolder.getView(R.id.tv_power);

            tvName.setText(subDevice.getSubDeviceName());
            loadTextColon(tvModel,"型号",subDevice.getModel());
            loadTextColon(tv_check_cycle,"点检周期",subDevice.getCheckPointCycle()+"天");
            loadTextColon(tvPower,"功率",subDevice.getPower());

            baseViewHolder.addOnClickListener(R.id.tv_detail);

        }
    }
}
